#define _CRT_SECURE_NO_WARNINGS 1

#include "Head.h"


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); )
        {
            if (nums[i] > 0) break;

            int target = -nums[i];
            int left = i + 1, right = nums.size() - 1;
            while (left < right)
            {
                int sum = nums[left] + nums[right];
                if (sum < target) left++;
                else if (sum > target) right--;
                else
                {
                    vv.push_back({ nums[i],nums[left],nums[right] });

                    left++;
                    while (left < right && nums[left] == nums[left - 1]) left++;
                }
            }
            ++i;
            while (i < nums.size() && nums[i] == nums[i - 1])
            {
                i++;
            }
        }
        return vv;
    }
};

int main()
{
    Solution s;
    vector<int> arr{ -1,0,0,4,4,1,1,2,-1,-4 };
    vector<vector<int>> vv = s.threeSum(arr);
    for (auto ee : vv)
    {
        for (auto e : ee)
        {
            cout << e << " ";
        }
        cout << endl;
    }

}